﻿// (c) Copyright Webber-Cross Software Ltd.
// This source is subject to the Microsoft Public License (Ms-PL).
// Please see http://go.microsoft.com/fwlink/?LinkID=131993 for details.
// All other rights reserved.

/*
  In App.xaml:
  <Application.Resources>
      <vm:ViewModelLocatorTemplate xmlns:vm="clr-namespace:ObeliskMvvmLightDemo.ViewModel"
                                   x:Key="Locator" />
  </Application.Resources>
  
  In the View:
  DataContext="{Binding Source={StaticResource Locator}, Path=ViewModelName}"
  
  OR (WPF only):
  
  xmlns:vm="clr-namespace:ObeliskMvvmLightDemo.ViewModel"
  DataContext="{Binding Source={x:Static vm:ViewModelLocatorTemplate.ViewModelNameStatic}}"
*/

using WebberCross.Obelisk.MvvmLightDemo.Navigation;
namespace WebberCross.Obelisk.MvvmLightDemo.ViewModel
{
    public class ViewModelLocator
    {
        private static MainViewModel _main;
        private static ExampleViewModel _example;

        /// <summary>
        /// Initializes a new instance of the ViewModelLocator class.
        /// </summary>
        public ViewModelLocator()
        {
            ////if (ViewModelBase.IsInDesignModeStatic)
            ////{
            ////    // Create design time view models
            ////}
            ////else
            ////{
            ////    // Create run time view models
            ////}

            CreateMain();
            CreateExample();
        }

        /// <summary>
        /// Gets the Main property.
        /// </summary>
        public static MainViewModel MainStatic
        {
            get
            {
                if (_main == null)
                {
                    CreateMain();
                }

                return _main;
            }
        }

        public static ExampleViewModel ExampleStatic
        {
          get
          {
            if (_example == null)
            {
              CreateExample();
            }

            return _example;
          }
        }

        /// <summary>
        /// Gets the Main property.
        /// </summary>
        [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Performance",
            "CA1822:MarkMembersAsStatic",
            Justification = "This non-static member is needed for data binding purposes.")]
        public MainViewModel Main
        {
            get
            {
                return MainStatic;
            }
        }

        [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Performance",
              "CA1822:MarkMembersAsStatic",
              Justification = "This non-static member is needed for data binding purposes.")]
        public ExampleViewModel Example
        {
          get
          {
            return ExampleStatic;
          }
        }

        /// <summary>
        /// Provides a deterministic way to delete the Main property.
        /// </summary>
        public static void ClearMain()
        {
            _main.Cleanup();
            _main = null;
        }

        public static void ClearExample()
        {
          _example.Cleanup();
          _example = null;
        }

        /// <summary>
        /// Provides a deterministic way to create the Main property.
        /// </summary>
        public static void CreateMain()
        {
            if (_main == null)
            {
              // Inject nav service dependency
                _main = new MainViewModel(new NavigationService());

                // Register View Model with TombstoneHelper
                PersistenceManager.Instance.Register("_main", _main);
            }
        }

        public static void CreateExample()
        {
          if (_example == null)
          {
            _example = new ExampleViewModel();

            // Register View Model with TombstoneHelper
            PersistenceManager.Instance.Register("_example", _example);
          }
        }

        /// <summary>
        /// Cleans up all the resources.
        /// </summary>
        public static void Cleanup()
        {
            ClearMain();
            ClearExample();
        }
    }
}